//============================================================================
// Name        : cpp_sendmail_mailsend.cpp
// Author      : darkflash
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <errno.h>

using namespace std;

int sendmail(const char *to, const char *from, const char *subject, const char *message)
{
    int retval = -1;
    FILE *mailpipe = popen("/usr/lib/sendmail -t", "w");
    if (mailpipe != NULL) {
        fprintf(mailpipe, "To: %s\n", to);
        fprintf(mailpipe, "From: %s\n", from);
        fprintf(mailpipe, "Subject: %s\n\n", subject);
        fwrite(message, 1, strlen(message), mailpipe);
        fwrite(".\n", 1, 2, mailpipe);
        pclose(mailpipe);
        retval = 0;
     }
     else {
         perror("Failed to invoke sendmail");
     }
     return retval;
}

int main(int argc, char** argv)
{
	cout << "input to: ";
	string input_to;
	cin >> input_to;

	cout << "input from: ";
	string input_from;
	cin >> input_from;

	cout << "input subject";
	string input_subject;
	cin >> input_subject;

	cout << "input message: ";
	string input_message;
	cin >> input_message;

    sendmail(input_to.c_str(), input_from.c_str(), input_subject.c_str(), input_message.c_str());
}
